/*-----------------------------------------------------------------------------
 * PROJ : spring-test
 * NAME : UserDao
 * DESC : 
 * Author : hosung
 * VER  : v1.0  * 
 * Copyright 2011 RF Window All rights reserved
 *-----------------------------------------------------------------------------
 *                  	Change History                       
 *-----------------------------------------------------------------------------
 *    DATE     AUTHOR                      DESCRIPTION                        
 * ----------  ------  -------------------------------------------------------- 
 * 2011. 5. 12.	hosung  First Write.                                     
 *-----------------------------------------------------------------------------
 */ 
package com.rfwindow.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.rfwindow.domain.UserData;
/**
 * @author hosung
 *
 */
public class UserDao {
	private ConnectionManager ConnectionManager; 
	
	public UserDao(ConnectionManager connMgr) {
		this.ConnectionManager = connMgr;
	}
	
	public void add(UserData userData) throws ClassNotFoundException, SQLException {
		Connection conn = ConnectionManager.makeConnection();
		PreparedStatement ps = 	conn.prepareStatement("insert into users(id, name, password) values(?,?,?)");
		
		ps.setString(1, userData.getId());
		ps.setString(2, userData.getName());
		ps.setString(3, userData.getPassword());

		ps.executeUpdate();

		ps.close();
		conn.close();
	}
	
	public UserData get(String id) throws SQLException, ClassNotFoundException {
		Connection conn =  ConnectionManager.makeConnection();
		PreparedStatement ps = 	conn.prepareStatement("Select id, name, password from users where id = ?");
		ps.setString(1, id);
		ResultSet rs = ps.executeQuery();;
		rs.next();
		
		UserData userdData = new UserData();
		userdData.setId(rs.getString("id"));
		userdData.setName(rs.getString("name"));
		userdData.setPassword(rs.getString("password"));
		
		rs.close();
		ps.close();
		conn.close();
	
		return userdData;
		
	}
	
}
